package com.jml.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import javax.sql.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DruidConfig {
	//主要实现web监控控制处理
	@Bean
	public ServletRegistrationBean duridServlet(){
		//进行druid监控配置处理操作
		ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
		//allow白名单
		servletRegistrationBean.addInitParameter("allow","127.0.0.1,127.0.0.1");
		//deny黑名单
		servletRegistrationBean.addInitParameter("deny","");
		//用户名
		servletRegistrationBean.addInitParameter("loginUsername","jml");
		//密码
		servletRegistrationBean.addInitParameter("loginPassword","jmlps");
		//是否可以进行重置操作
		servletRegistrationBean.addInitParameter("resetEnable","false");
		return servletRegistrationBean;
	}

	@Bean
	public FilterRegistrationBean  filterRegistrationBean(){
		FilterRegistrationBean  filterRegistrationBean = new FilterRegistrationBean();
		filterRegistrationBean.setFilter(new WebStatFilter());
		//对所有的请求进行监控处理
		filterRegistrationBean.addUrlPatterns("/*");
		//排除不需要做监控的路径
		filterRegistrationBean.addInitParameter("exclusions","*.js,*.jpg,*.gif,*.css,/druid/*");
		return   filterRegistrationBean;
	}

	@Bean
	@ConfigurationProperties(prefix = "spring.datasource")
	public DataSource druidDataSource(){
		return new DruidDataSource();
	}

}
